package com.riber.student.util;

import com.riber.student.common.Const;
import com.riber.student.vo.output.LoginInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * @author: Riber
 * @date: 2022/10/26 10:43
 * @description:
 */
public class TokenUtils {

    public static String getToken(LoginInfoVo infoVo, Long times){
        long l = System.currentTimeMillis();
        //1 生成token 字符串
        return Jwts.builder()
                .claim("type", infoVo.getType())
                .setId(infoVo.getUserId().toString())
                .setSubject(infoVo.getUsername())
                .setIssuedAt(new Date(l))
                .setExpiration(new Date(l + times))
                .signWith(SignatureAlgorithm.HS256, Const.TOKEN_SIGN)
                .compact();
    }

    /**
     * @Description: 解码
     * @author: Riber
     * @date: 2022/10/26 17:35
     * @param: [token]
     * @return: io.jsonwebtoken.Claims
    **/
    public static Claims parseToken(String token){
        return (Claims) Jwts.parser()
                .setSigningKey(Const.TOKEN_SIGN)
                .parse(token).getBody();
    }
}

